const connection = require("../app/database")

class GoodsService {
  async inser(goods) {
    const {iid,desc,imgURL,newPrice,title} = goods;
    const statement = `INSERT INTO goods (iid,\`desc\`,imgURL,newPrice,title)VALUE(?,?,?,?,?);`;
    const result = await connection.execute(statement, [iid,desc,imgURL,newPrice,title]);
    return result;
  }

  async user_buy_goods(account,iid,num){
    const statement = `INSERT INTO user_buy_detail(account,iid,num)VALUES(?,?,?);`;
    const result = await connection.execute(statement,[account,iid,num])
    return result;
  }

  async user_search_goods(account){
    const statement = `SELECT goods.iid,goods.\`desc\`,goods.imgURL,goods.newPrice,goods.title,
    user_buy_goods.num
    FROM user
    LEFT JOIN user_buy_goods ON user.account = user_buy_goods.account
    LEFT JOIN goods ON user_buy_goods.iid = goods.iid
    WHERE user.account = ?;`;
    const result = await connection.execute(statement,[account])
    return result[0];
  }
}

module.exports = new GoodsService();